﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace JHSoft.Extension
{
    public static class StringExtension
    {
        /// <summary>
        /// String 类扩展方法：判断是否为空！
        /// </summary> 
        public static bool IsNullOrEmpty()
        {
            return IsNullOrEmpty(null);
        }

        /// <summary>
        /// String 类扩展方法：判断是否为空！
        /// </summary>
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }
        //示例代码：
        //         public static void Test1()
        //         {
        //             string s = "";
        //             bool b1 = string.IsNullOrEmpty(s);
        //            bool b2 = s.IsNullOrEmpty();
        //        }
        public static bool IsMatch(this string str, string pattern)
        {
            if (str == null) return false;
            else return Regex.IsMatch(str, pattern);
        }

        public static string Match(this string str, string pattern)
        {
            if (str == null) return "";
            return Regex.Match(str, pattern).Value;
        }

        //示例代码：
        //public static void Test3()
        //{
        //    bool b = "12345".IsMatch(@"\d+");
        //    string s = "ldp615".Match("[a-zA-Z]+");
        //}

        public static string FormatWith(this string format, params object[] args)
        {
            return string.Format(format, args);
        }

        //示例代码：
        //public static void Test2()
        //{
        //    string today = "今天是：{0:yyyy年MM月dd日 星期ddd}".FormatWith(DateTime.Today);
        //}


        public static string ToCamel(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToLower() + s.Substring(1);
        }

        public static string ToPascal(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToUpper() + s.Substring(1);
        }
    }
}
